#include "mainscene.h"

//1600*900的窗口，地面在730的位置



mainscene::mainscene(QWidget *parent) : QMainWindow(parent)
{

    setFixedSize(1600,900);
    setWindowTitle("i wanna to escape India");

    for(int i=0;i<81;i++)
    {
        brick[i]=QRect(20*i,730,20,200);
    }
    backgro1.setHeight(900);
    backgro1.setWidth(1600);
    backgro2=backgro1;
    backgro1.moveTo(0,0);
    backgro2.moveTo(1600,0);
    //初始化随机数种子
    srand((unsigned int)time(NULL));

    playgame();
}

void mainscene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    painter.drawPixmap(backgro1,QPixmap(":/imagine/background.jpg"));
    painter.drawPixmap(backgro2,QPixmap(":/imagine/background.jpg"));
    if(winornot==true&&backgro2.left()==1600)
    {
        bacx=1600;
    }
    if(bacx!=-3000)
    {
        painter.drawPixmap(bacx,0,QPixmap(":/imagine/win.jpg"));

    }
    for(int i=0;i<81;i++)
    {
        painter.drawPixmap(brick[i],QPixmap(":/imagine/brick.png"));
    }
    painter.drawPixmap(mas.mX,mas.mY,mas.m_master);
    painter.drawPixmap(bot1.nX,bot1.nY,bot1.bot1_bottle);
    painter.drawPixmap(man.rect,man.p_people);
}


void mainscene::playgame()
{

   timeEnemy.start(8);
   QLabel *lab1=new QLabel;
   lab1->setGeometry(0,25,160,90);
   lab1->setParent(this);
   QFont ft;
   ft.setPointSize(14);
   lab1->setFont(ft);
   lab1->show();


   QSound *music= new QSound(":/imagine/Tunak Tunak Tun.WAV");
   music->play();

   connect(&timeEnemy,QTimer::timeout,[=](){

       score=score+0.01;
       QString str1;
       str1="分数：";
       lab1->setText(str1+QString::number(score));

       if(score>=80)
       {
           winornot=true;
       }

       updatePosition();

       update();
       //胜利事件
       if(bacx==2)
       {
           timeEnemy.stop();
           QMessageBox *msg1=new QMessageBox;
           msg1->setWindowTitle("   unbielivable     !   you are win     !");
           msg1->setText("you have managed to escape India and load the ship to China");
           msg1->setStandardButtons(QMessageBox::Yes);
           int ret1 = msg1->exec();
           switch (ret1) {
               case QMessageBox::Yes:
               this->close();
                   break;
               default:
                   break;
             }

       }
       //判断是否相撞
       if(collisionDetection())
       {
           timeEnemy.stop();
           music->stop();
           QMessageBox *msg=new QMessageBox;
           msg->setWindowTitle("Game Over");
           msg->setText("you are dead in India Do you want to try again?");
           msg->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
           msg->setDefaultButton(QMessageBox::Yes);
           mainscene *gameScene2 = new mainscene;
           int ret = msg->exec();
           switch (ret) {
               case QMessageBox::Yes:
               this->close();
               gameScene2->show();

                   break;
               case QMessageBox::No:
                   this->close();
                   break;

               default:
                   break;
             }



       }
       if(winornot==true)
       {
           music->stop();
       }

   });


   QTimer *timer = new QTimer(this);
   timer->start(50);
   connect(timer,QTimer::timeout,[=](){
       man.updatePosition();
       man.presskey=0;
   });
}

void mainscene::updatePosition()
{
    //背景的移动
    if(bacx>=0)
    {
        bacx-=2;
    }
    else
    {
        if(bacx==-3000)
            bacx=-3000;
        else
            bacx=1600;
    }
    if(backgro1.left()>=-1600)
    {
        backgro1.setLeft(backgro1.left()-2);
        backgro1.setRight(backgro1.right()-2);
    }
    else
    {
        backgro1.setLeft(0);
        backgro1.setRight(1600);
        if(backgro1.left()==-3000)
        {
            backgro1.setLeft(-3000);
            backgro1.setRight(-1400);
        }
    }
    if(backgro2.left()>=0)
    {
        backgro2.setLeft(backgro2.left()-2);
        backgro2.setRight(backgro2.right()-2);
    }
    else
    {
        backgro2.setLeft(1600);
        backgro2.setRight(3200);
    }
    if(winornot==false){
    barrierToScene();
    }
    if(mas.state==true)
    {
        mas.updatePosition();
    }
    if(bot1.state==true)
    {
        bot1.updatePosition();
    }


    //让地面动起来
    for(int i=0;i<81;i++)
    {
        QRect temp=brick[i];
        if(brick[i].right()>=0)
        {
            brick[i].setLeft(temp.left()-2);
            brick[i].setRight(temp.right()-2);
        }
        else
        {
            brick[i].setLeft(1600);
            brick[i].setRight(1620);
        }
    }


}

void mainscene::barrierToScene()
{
    int showup=rand()%300;
    if(mas.state==false&&bot1.state==false){
        switch (showup) {
        case 1:
            mas.born();
            mas.state=true;
            break;
        case 2:
            bot1.born();
            bot1.state=true;
        default:
            break;
        }
    }
    else if(bot1.state==false&&mas.state==true&&mas.mX<600)
    {
        if(showup==1)
        {
            bot1.born();
            bot1.state=true;
        }
    }
    else if(mas.state==false&&bot1.state==true&&bot1.nX<600)
    {
        if(showup==1)
        {
            mas.born();
            mas.state==true;
        }
    }
}

void mainscene::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch (key->key()) {
    case Qt::Key_A:
        man.presskey=1;
        break;
    case Qt::Key_S:
        man.presskey=2;
        if(key->key()==Qt::Key_K&&key->key()==Qt::Key_S)man.presskey=0;
        break;
    case Qt::Key_D:
        man.presskey=3;
        break;
    case Qt::Key_K:
        man.presskey=4;
        break;

    default:
        man.presskey=0;
        break;
    }
}

bool mainscene::collisionDetection()  // 撞上为true
{
    bool result=false;
    if(man.rect.intersects(mas.mrect))
    {
        result=true;
    }
    if(man.rect.intersects(bot1.mrect))
    {
        result=true;
    }
    return result;

}
